{{template "shared/bootstrap_table_js.html"}}

<script>
    var $dataGrid = $('#dataGrid');
    var $searchForm = $('#searchForm');

    //高亮显示
    var highlightPk = null;

    $(function () {
        pageInit()
    });

    function pageInit() {
        sdtheme.loadSearchText('courseSearchForm');

        dataGridInit();

        $('#btnSearch').on('click', function (e) {
            refreshToFirstPage();
        });

        $('#btnClearSearch').on('click', function (e) {
            $('select', $searchForm).selectpicker('val', '');
            $('input', $searchForm.val(''));
            refreshToFirstPage();
        });

        $dataGrid.on('load-success.bs.table', function () {
            if (highlightPk !== null && typeof highlightPk !== 'undefined') {
                sdtheme.highlight($dataGrid.find('tbody tr[data-pk="' + highlightPk + '"]'));
            }
            //高亮显示一次后失效
            highlightPk = null;
        });
        //保持搜索条件面板状态
        sdtheme.searchPanelStatusInit('searchpanelbtn');
    }

    function dataGridInit() {
        $dataGrid.bootstrapTable({
            url: '{{urlfor "CourseCommentController.DataGrid"}}',
            method: 'post',
            sidePagination: 'server',
            idField: 'Id',
            queryParams: function (params) {
                params.CourseName = $.trim($('.searchForm input[name="CourseName"]').val());
                return params;
            },
            pagination: true,
            showRefresh: true,
            showColumns: true,
            toolbar: '#toolbar',
            pageSize: 10,
            pageList: [5, 10, 20, 100, 200],
            paginationShowPageGo: true,//20170812 lht 扩展select跳转
            paginationUseBSSelect: true,//20170812 lht 扩展select跳转 启用BoostrapSelect(页面须引用botstrap-select.min.js)
            cookie: true,
            classes: 'table table-bordered table-hover',
            undefinedText: '',
            sortName: 'Id',
            sortOrder: 'desc',
            stickyHeader: true,
            resizable: true,

            rowAttributes: function (row, index) {
                return {'data-pk': row.Id}
            },
            columns: [{
                field: 'state',
                checkbox: true,
                radio: false
            }, {
                field: 'Id',
                title: 'ID',
                width: '80px',
                align: 'center',
                sortable: true
            }, {
                field: 'Course.CourseName',
                title: '课程名称'
            }, {
                field: 'Content',
                title: '内容'
            }, {
                field: 'Status',
                title: '状态',
                formatter: function (value, row, index) {
                    var status = '';
                    switch (value) {
                        case -1:
                            status = '<label class="label label-danger label-sm">已屏蔽</label>';
                            break;
                        case 1:
                            status = '<label class="label label-success label-sm">显示</label>';
                            break;
                        case 0:
                            status = '<label class="label label-warning label-sm">未审核</label>';
                            break;
                        default:
                            status = '<label class="label label-warning label-sm">未审核</label>';
                            break;
                    }
                    return status;
                }
            }],
            onLoadSuccess: function (data) {
                layer.closeAll('loading')
            },
            onSort: function (name, order) {

            },
            onCheckAll: function (rows) {
                $(".table-btn-check-enable").removeAttr("disabled")
            },
            onCheck: function (rows) {
                $(".table-btn-check-enable").removeAttr('disabled');
            },
            onUncheck: function (rows) {
                if ($("#dataGrid").bootstrapTable("getSelections").length === 0) {
                    $(".table-btn-check-enable").attr('disabled', 'disabled');
                }
            },
            onUncheckAll: function () {
                //禁用删除需要勾选才能用的按钮
                if ($("#dataGrid").bootstrapTable("getSelections").length === 0) {
                    $(".table-btn-check-enable").attr('disabled', 'disabled');
                }
            },
            onLoadError: function (status) {
                layer.alert('获取数据失败,错误代码：' + status);
            }
        });
    }

    function refresh(pk) {
        $dataGrid.bootstrapTable('refresh');
        highlightPk = pk;
    }
    
    function refreshToFirstPage() {
        //表格刷新时，保存查询条件到cookie
        sdtheme.saveSearchText('courseSearchForm');
        //刷新并跳转至第1页
        $dataGrid.bootstrapTable('refresh', {pageNumber: 1});
        //同时要更新cookie里保存当前页码
        $.cookie('dataGrid.bs.table.pageNumber', 1, {expires: 1});
    }

    function DeleteFun() {
        var url = '{{urlfor "CourseCommentController.Delete"}}';
        var rows = $dataGrid.bootstrapTable("getSelections");
        if (rows.length === 0) {
            layer.alert("请选择想要删除的项", {icon: 0, title: '提示'});
            return;
        }

        var ids = [];
        $(rows).each(function (i, e) {
            ids.push(e.Id)
        });

        layer.confirm('您是否删除所选的项？', {
            btn: ['是', '否'], icon: 3, title: '请确认'
        }, function () {
            var params = {ids: ids.join(',')};
            $.sdpost(url, params, function (res) {
                if (res.code === 0) {
                    layer.msg(res.msg, {icon: 1, title: '成功'});
                    refresh();
                } else {
                    layer.alert(res.msg, {icon: 2, title: '失败'})
                }
            })
        })
    }

    function AuditFun(status) {
        var url = '{{urlfor "CourseCommentController.Audit"}}';
        var rows = $dataGrid.bootstrapTable('getSelections');

        if (rows.length === 0) {
            layer.alert("请选择想要删除的项", {icon: 0, title: '提示'});
            return;
        }
        var ids = [];
        $(rows).each(function (i, e) {
            ids.push(e.Id)
        });

        layer.confirm('您是否删除所选的项？', {
            btn: ['是', '否'], icon: 3, title: '请确认'
        }, function () {
            var params = {ids: ids.join(','), status: status};
            $.sdpost(url, params, function (res) {
                if (res.code === 0) {
                    layer.msg(res.msg, {icon: 1, title: '成功'});
                    refresh();
                } else {
                    layer.alert(res.msg, {icon: 2, title: '失败'})
                }
            })
        })
    }
</script>